package com.xjtu.duansheli.controller;


import com.xjtu.duansheli.entity.CategoriesInfo;
import com.xjtu.duansheli.entity.ProductInfo;
import com.xjtu.duansheli.entity.UserInfo;
import com.xjtu.duansheli.json.AjaxResponse;
import com.xjtu.duansheli.service.CategoriesInfoService;
import com.xjtu.duansheli.service.ProductInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */
@Slf4j
@RestController
@RequestMapping("/project/categories-info")
@Api(value = "商品类别信息管理模块", description = "这是商品类别信息增删改查的模块")
public class CategoriesInfoController {

    @Autowired
    private CategoriesInfoService categoriesInfoService;


    @ApiOperation(value = "获取全部商品类别", notes = "获取所有商品类别的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getCategories", method = RequestMethod.GET)
    public AjaxResponse getAll(@RequestParam(value = "current") Integer current,
                               @RequestParam(value = "size") Integer size) {
        return AjaxResponse.success(categoriesInfoService.getAllCategories(current,size));
    }


    @ApiOperation(value = "根据id查找商品类别", notes = "获取根据id查找到的商品类别")
    @ApiImplicitParam(name = "cid", value = "商品类别id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getCategoriyById", method = RequestMethod.GET)
    public AjaxResponse getCategoryById(@RequestParam(name = "cid") Long cid) {
        return AjaxResponse.success(categoriesInfoService.getCategoryByid(cid));
    }

    @ApiOperation(value = "添加商品类别信息", notes = "获取商品类别信息并且添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cName", value = "商品类别名", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "备注", required = false, paramType = "form", dataType = "String"),
    })
    @RequestMapping(value = "/addCategory", method = RequestMethod.POST)
    public AjaxResponse addCategory(@RequestParam(value = "cName") String cName,
                                   @RequestParam(value = "remark", defaultValue = "无备注") String remark,
                                   HttpServletRequest request) {
        UserInfo user = (UserInfo) request.getSession().getAttribute("loginUser");
        CategoriesInfo categoriesInfo = new CategoriesInfo()
                .setCname(cName)
                .setRemark(remark)
                .setAdderId(user.getUid());
        return AjaxResponse.success("数据库受影响行数->" + categoriesInfoService.addCategory(categoriesInfo));
    }

    @ApiOperation(value = "修改商品类别信息", notes = "录入新的商品类别信息并且修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cId", value = "要修改的商品类别id", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "cName", value = "商品类别名", required = false, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "备注", required = false, paramType = "form", dataType = "String"),
    })
    @RequestMapping(value = "/updateCategory", method = RequestMethod.PUT)
    public AjaxResponse updateCategory(@RequestParam(value = "cId") Long cId,
                                       @RequestParam(value = "cName") String cName,
                                       @RequestParam(value = "remark", defaultValue = "无备注") String remark,
                                       HttpServletRequest request) {
        UserInfo user = (UserInfo) request.getSession().getAttribute("loginUser");
        CategoriesInfo categoriesInfo = new CategoriesInfo()
                .setCid(cId)
                .setCname(cName)
                .setRemark(remark)
                .setAdderId(user.getUid());
        return AjaxResponse.success("数据库受影响行数->" + categoriesInfoService.updateCategory(categoriesInfo));
    }

    @ApiOperation(value = "删除商品类别信息", notes = "从数据库中删除某个商品类别")
    @ApiImplicitParam(name = "cid", value = "要删除的商品类别id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteCategory", method = RequestMethod.DELETE)
    public AjaxResponse deleteCategory(@RequestParam(value = "cid") Long cid) {
        return AjaxResponse.success("数据库受影响行数->" + categoriesInfoService.deleteCategory(cid));
    }

    @ApiOperation(value = "商品类别总数量", notes = "获取数据库中商品类别总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(categoriesInfoService.getTotal());
    }
}

